package com.risen.project.enums;

import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.Optional;

/**
 * 建设类型枚举
 */
public enum ConstructionTypeEnum {

    CTE_SOFTWARE("1", "软件"),

    CTE_HARDWARE("2", "硬件"),

    CTE_SERVICE("3", "服务")
    ;

    private final String code;
    private final String msg;

    ConstructionTypeEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static String getMsgByCode(String code) {
        Optional<ConstructionTypeEnum> optional = Arrays.stream(values())
                .filter(c -> c.code.equals(code))
                .findFirst();

        return optional.isPresent() ? optional.get().msg : "";
    }

    /**
     * 分割code拼接枚举结果方法
     * @param code 匹配code
     * @param split 分隔符
     * @return String
     */
    public static String getMsgByCodeSplit(String code, String split) {
        if (StrUtil.isEmpty(code)) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        String[] codeSplit = StrUtil.split(code, split);

        for (String cs : codeSplit) {
            Optional<ConstructionTypeEnum> optional = Arrays.stream(values())
                    .filter(c -> c.code.equals(cs))
                    .findFirst();

            optional.ifPresent(typeEnum -> sb.append(typeEnum.msg).append(split));
        }

        return sb.toString();
    }
}
